package com.rr;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.rr.frame.filter.JwtAuthTokenFilter;

@Configuration
@EnableWebMvc
public class CorsConfig extends WebMvcConfigurerAdapter {

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/api/**")
			.allowedOrigins("http://localhost:8080")
			.allowedMethods("*")
			.allowedHeaders("*")
			.allowCredentials(false).maxAge(3600);
		
		registry.addMapping("/openApi/**")
		.allowedOrigins("*")
		.allowedMethods("*")
		.allowedHeaders("*")
		.allowCredentials(false).maxAge(3600);
	}
//	
	@Bean
	public FilterRegistrationBean getJwtAuthTokenFilter(){
		JwtAuthTokenFilter jwtFilter=new JwtAuthTokenFilter();
		FilterRegistrationBean registrationBean=new FilterRegistrationBean();
		registrationBean.setFilter(jwtFilter);
		List<String> urlPatterns=new ArrayList<String>();
		urlPatterns.add("/api/*");//拦截路径，可以添加多个
		registrationBean.setUrlPatterns(urlPatterns);
		registrationBean.setOrder(1);
		return registrationBean;
	}
	
	
//	@Bean
//	 public FilterRegistrationBean greetingFilterRegistrationBean() {
//	 FilterRegistrationBean registrationBean = new FilterRegistrationBean();
//	 registrationBean.setName("greeting");
//	 JwtAuthTokenFilter greetingFilter = new JwtAuthTokenFilter();
//	 registrationBean.setFilter(greetingFilter);
//	 registrationBean.setOrder(1);
//	 List<String> urlList = new ArrayList<String>();
//	 urlList.add("/api/**");
//	 registrationBean.setUrlPatterns(urlList);
//	 return registrationBean;
//	 }
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
	    registry.addResourceHandler("/**")
	            .addResourceLocations("classpath:/static/")
	            .addResourceLocations("file:E:/");
//	    registry.addResourceHandler("/**")
//        .addResourceLocations("file:E:/");
	        
	}
	
	
}

